package com.ajun.notice;

import com.alibaba.fastjson.JSON;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RobotNotice {

    private static final String CONTENT_TYPE_KEY = "Content-Type";

    private static final String CONTENT_TYPE_VALUE = "application/json; charset=utf-8";

    private static final String CONTENT_TYPE = "text/json";

    private static final String MSG_TYPE = "text";

    private String talkUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/d2f6cdc2-acec-4939-889d-a0a413324fd1";

    private String secretKey = "cbA7deuezbVgdh2tmmbHDc";

    /**
     * 发送通知
     *
     * @param msg
     */
    private void doSend(NoticeMsg msg) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(talkUrl);
        CloseableHttpResponse httpResponse = null;
        try {
            httpPost.setHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE);
            StringEntity se = new StringEntity(JSON.toJSONString(msg), CharsetUtil.UTF_8);
            se.setContentType(CONTENT_TYPE);
            httpPost.setEntity(se);
            httpResponse = httpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                log.info("发送成功！");
            }
            HttpEntity httpEntity = null;
            String result = null;
            if (null != httpResponse) {
                httpEntity = httpResponse.getEntity();
                result = httpEntity == null ? null : EntityUtils.toString(httpEntity, CharsetUtil.UTF_8);
                EntityUtils.consumeQuietly(httpEntity);
                httpResponse.close();
            }
        } catch (Exception e) {
            log.error("信息发送失败, 错误信息", e);
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.error("连接httpClient关闭异常, 错误信息", e);
                }
            }
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    log.error("httpResponse关闭异常, 错误信息", e);
                }
            }
        }
    }

    /**
     * 发送通知
     *
     * @param message
     * @throws Exception
     */
    public void sendMessage(String message) throws Exception {
        long timestamp = System.currentTimeMillis() / 1000;
        String sign = FeishuSignUtil.getSignStr(secretKey, timestamp);
        NoticeContent conent = new NoticeContent();
        conent.setText(message);
        NoticeMsg msg = new NoticeMsg();
        msg.setMsgType(MSG_TYPE);
        msg.setContent(conent);
        msg.setSign(sign);
        msg.setTimestamp(String.valueOf(timestamp));
        doSend(msg);
    }

}
